package com.centaline.androidsalesblog.api;

import android.support.v4.util.ArrayMap;

import com.android.volley.Request;
import com.centaline.androidsalesblog.BuildConfig;
import com.centaline.androidsalesblog.base.BaseApi;
import com.centaline.androidsalesblog.model.responseobject.RongTokenRo;
import com.centanet.framework.interfaces.IResponseListener;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * 融云token
 * Created by guilin on 16/3/16.
 */
public class RongTokenApi extends BaseApi<RongTokenRo> {

    final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    /*测试*/
    final String key_alhpa = "p5tvi9dst2r54";
    final String secret_alhpa = "oiDItffTZj";
    /*线上*/
    final String key_release = "n19jmcy59f619";
    final String secret_release = "178n2EQNgoipC";

    private String userId;
    private String name;
    private String portraitUri;
    private boolean debug;

    public RongTokenApi(Class<?> tag, Class<RongTokenRo> aClass, IResponseListener<RongTokenRo> iResponseListener) {
        super(tag, aClass, iResponseListener);
//        debug = false;
        debug = BuildConfig.DEBUG;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPortraitUri(String portraitUri) {
        this.portraitUri = portraitUri;
    }

    @Override
    protected int getMethod() {
        return Request.Method.POST;
    }

    @Override
    protected ArrayMap<String, String> getHeaders() {
        ArrayMap<String, String> map = new ArrayMap<>();
        final String nonce = String.valueOf(new Random().nextInt(Integer.MAX_VALUE));
        final String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        map.put("App-Key", debug ? key_alhpa : key_release);
        map.put("Nonce", nonce);
        map.put("Timestamp", timestamp);
        map.put("Signature", signature(debug ? secret_alhpa : secret_release, nonce, timestamp));
        return map;
    }

    @Override
    protected void setParams() {
        putStringParams("userId", userId);
        putStringParams("name", name);
        putStringParams("portraitUri", portraitUri);
    }

    @Override
    protected String getIp() {
        return "http://api.cn.rong.io/user/getToken.json";
    }

    @Override
    protected String getPath() {
        return "";
    }

    @Override
    protected String getBodyContentType() {
        return "application/x-www-form-urlencoded";
    }

    private String signature(String secret, String nonce, String timestamp) {
        final String input = secret + nonce + timestamp;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(input.getBytes());
            byte[] bytes = messageDigest.digest();
            int len = bytes.length;
            char[] resultCharArray = new char[len * 2];
            int index = 0;
            for (byte b : bytes) {
                resultCharArray[index++] = HEX_DIGITS[b >>> 4 & 0xf];
                resultCharArray[index++] = HEX_DIGITS[b & 0xf];
            }
            return new String(resultCharArray);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}
